package com.wenwu.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wenwu.common.bean.RelicsAttachment;
import com.wenwu.common.bean.RelicsProtectArea;
import com.wenwu.common.bean.RelicsProtectBasic;
import com.wenwu.common.exception.RelicsException;
import com.wenwu.common.page.PaginationVO;
import com.wenwu.common.utils.Status.AttachmentFrom;
import com.wenwu.dao.RelicsAttachmentMapper;
import com.wenwu.dao.RelicsBasicMapper;
import com.wenwu.dao.RelicsProtectAreaMapper;
import com.wenwu.dao.RelicsProtectBasicMapper;
import com.wenwu.service.ProtectAreaService;

@Service
public class ProtectAreaServiceImpl implements ProtectAreaService {

	@Autowired
	private RelicsProtectAreaMapper relicsProtectAreaMapper;
	@Autowired
	private RelicsBasicMapper basicMapper;
	@Autowired
	private RelicsProtectBasicMapper rpbMapper;
	@Autowired
	private RelicsAttachmentMapper attachmentMapper;
	Logger log = Logger.getLogger(this.getClass());

	public String addObj(RelicsProtectArea relicsProtectArea)
			throws RelicsException {
		try {
			int upRecordNum = 0;
			if (relicsProtectArea.getProtectId() == null) {
				upRecordNum = relicsProtectAreaMapper
						.insertSelective(relicsProtectArea);
				if (upRecordNum > 0) {
					return "保存现状添加成功";
				} else {
					return "保存现状添加失败";
				}
			} else {
				upRecordNum = relicsProtectAreaMapper
						.updateByPrimaryKeySelective(relicsProtectArea);
				if (upRecordNum > 0) {
					return "保存现状修改成功";
				} else {
					return "保存现状修改失败";
				}
			}
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}

	}

	public int selectTotalRecord(RelicsProtectArea relicsProtectArea)
			throws RelicsException {
		return relicsProtectAreaMapper.selectTotalRecord(relicsProtectArea);
	}

	public PaginationVO selectList(RelicsProtectArea relicsProtectArea,
			PaginationVO pageVo) throws RelicsException {
		try {
			pageVo.setTotalRecord(relicsProtectAreaMapper
					.selectTotalRecord(relicsProtectArea));
			pageVo.setList(relicsProtectAreaMapper.selectList(
					relicsProtectArea, pageVo));
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
		return pageVo;

	}

	public RelicsProtectArea selectProtectAreaByProtectId(Integer protectId)
			throws RelicsException {
		return relicsProtectAreaMapper.selectByPrimaryKey(protectId);
	}

	public RelicsProtectArea selectProtectAreaByBasicId(Integer basicId)
			throws RelicsException {
		try {
			return relicsProtectAreaMapper
					.selectRelicsProtectAreaByBasicId(basicId);
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
	}

	public int deleteByKey(List<?> keys) throws RelicsException {
		try {
			return relicsProtectAreaMapper.deleteByKey(keys);
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
	}

	public int saveData(RelicsProtectArea relicsProtectArea,
			List<RelicsProtectBasic> list,List<RelicsAttachment> ralist) throws RelicsException {
		int result = 0;
		try {
			for (RelicsProtectBasic rpb : list) {
				if (rpb.getProtectBasicId() == null) {
					result += rpbMapper.insertSelective(rpb);
				} else {
					result += rpbMapper.updateBySelective(rpb);
				}
				if (rpb.getProtectBasicId() != null) {
					if (rpb.getFlag().equals("flag1")) {
						relicsProtectArea.setProtectBasicId(rpb
								.getProtectBasicId());
					} else if (rpb.getFlag().equals("flag2")) {
						relicsProtectArea.setProtectArea(rpb
								.getProtectBasicId());
					} else if (rpb.getFlag().equals("flag3")) {
						relicsProtectArea.setControlArea(rpb
								.getProtectBasicId());
					}
				}
			}
			if (relicsProtectArea.getProtectId() == null) {
				result += relicsProtectAreaMapper
						.insertSelective(relicsProtectArea);
				if (relicsProtectArea.getProtectId() > 0) {
					if (ralist != null && ralist.size() > 0) {
						for (RelicsAttachment attachment : ralist) {
							attachment.setAttachmentDesc("保存现状附件");
							attachment.setTableOwner(relicsProtectArea
									.getProtectId());// 外键
						}
						result += attachmentMapper.insertList(ralist);
					}
				}
			} else {
				result += relicsProtectAreaMapper
						.updateByPrimaryKeySelective(relicsProtectArea);
				List<Integer> owns = new ArrayList<Integer>();
				owns.add(AttachmentFrom.RELICS_PROTECT_AREA_01);
				result += attachmentMapper.deleteByKey(
						relicsProtectArea.getProtectId(), owns);// 先删除原有的附件，再添加
				if (relicsProtectArea.getProtectId() > 0) {
					if (ralist != null && ralist.size() > 0) {
						for (RelicsAttachment attachment : ralist) {
							attachment.setAttachmentDesc("保存现状附件");
							attachment.setTableOwner(relicsProtectArea
									.getProtectId());// 外键
						}
						result += attachmentMapper.insertList(ralist);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RelicsException("系统异常");
		}
		return result;
	}

	public List<RelicsProtectBasic> selectProtectBasicList(List<Integer> list)
			throws RelicsException {
		try {
			return rpbMapper.selectProtectBasicList(list);
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
	}
}